<?php
/**
 * @file name : filemodel_model.php
 * @package   :
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class Filemodel_model extends Base_Model
{
    /**
     * @function __construct
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * @function createModelFile
     *
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access   public
     * @param    array
     * @param    string
     * @param    string
     * @return
     */
    public function createModelFile($arrModule = array(), $projectName, $templateKind)
    {
        $arrModelAutoload = array();
        // read model_default file
        $filename = APPPATH . 'model_default/model_default.php';
        $fp = fopen($filename, 'r');
        $buff = '';
        if ($fp) {
            while ( ! feof($fp)) {
                $buff .= fgets($fp);
            }
        }
        fclose($fp);
        foreach ($arrModule as $key => $module) {
            if ( ! is_null($module->m_tbl_name) && strlen($module->m_tbl_name) > 0 &&
                 $module->m_create == 'Y') {

                $fileModel = preg_replace('/_/', '', $module->m_name);
                $arrReplace   = array('\<MODEL_NAME\>'      => $fileModel,
                                      '\<PROJ_NAME\>'       => $projectName,
                                      '\<CPR\>'         	=> date('Y'),
                                      '\<MODEL_CLSS_NAME\>' => ucfirst($fileModel),
                                      '\<PRI_TBL_NAME\>'    => $module->m_tbl_name,
                                      '\<PRI_TBL_ID\>'      => $module->tbl_pri_id);

                array_push($arrModelAutoload, $fileModel . '_model');
                $filename = PROJECT_PATH . $projectName . '/administrator/application/models/' . $fileModel . '_model.php';
                $fp = fopen($filename, 'w');
                $buff1 = $buff;
                foreach ($arrReplace as $repKey => $val) {
                    $buff1 = preg_replace('/' . $repKey . '/', $val, $buff1);
                }

                fwrite($fp, $buff1);
                fclose($fp);

            } else {
                // Get modules with parent_id in this module id
                $arrWhere = array($module->m_id);
                $arrChildModule = $this->modules_model->loadListIn('parent_id', $arrWhere);
                foreach ($arrChildModule as $childKey => $childModule) {
                    if ( ! is_null($childModule->m_tbl_name) && strlen($childModule->m_tbl_name) > 0
                        && $childModule->m_create == 'Y') {

                        $fileModel = preg_replace('/_/', '', $childModule->m_name);
                        $arrReplace   = array('\<MODEL_NAME\>'      => $fileModel,
                                              '\<PROJ_NAME\>'       => $projectName,
                                              '\<CPR\>'         	=> date('Y'),
                                              '\<MODEL_CLSS_NAME\>' => ucfirst($fileModel),
                                              '\<PRI_TBL_NAME\>'    => $childModule->m_tbl_name,
                                              '\<PRI_TBL_ID\>'      => $childModule->tbl_pri_id);

                        array_push($arrModelAutoload, $fileModel . '_model');
                        $filename = PROJECT_PATH . $projectName . '/administrator/application/models/' . $fileModel . '_model.php';
                        $fp = fopen($filename, 'w');
                        $buff2 = $buff;
                        foreach ($arrReplace as $repKey => $val) {
                            $buff2 = preg_replace('/' . $repKey . '/', $val, $buff2);
                        }

                        fwrite($fp, $buff2);
                        fclose($fp);
                    }
                }
            }
        }
        /**
         * Add model to autoload on autoload.php file
         */
        if (count($arrModelAutoload) > 0) {
            $filename = PROJECT_PATH . $projectName . '/administrator/application/config/autoload.php';
            $fp = fopen($filename, 'r');
            $buffAuto = '';
            $strReplace = ',';
            foreach ($arrModelAutoload as $k => $modeStr) {
                $strReplace .= '\'' . $modeStr . '\', ';
            }
            if (strlen($strReplace) > 0) {
                $strReplace = substr($strReplace, 0, strrpos($strReplace, ','));
            }
            if ($fp) {
                while (! feof($fp)) {
                    $buffAuto .= fgets($fp);
                }
            }
            fclose($fp);
            /**********/
            $fp = fopen($filename, 'w');
            if ($fp) {
                if (strlen($buffAuto) > 0) {
                    $buffAuto = sprintf($buffAuto, $projectName, date('Y'));
                    $buffAuto = preg_replace('/\); \/\/ add more here/', $strReplace . ');', $buffAuto);
                    fwrite($fp, $buffAuto);
                }
            }
            fclose($fp);
        }
    }
}